package timberman.models;

import java.util.Random;

import timberman.core.AssetUtils;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array;

public class Tree extends Group {
	boolean syn = false;
	Random random = new Random();
	Skin skin = AssetUtils.skin;
	Array<Wood> trees = new Array<Wood>();
	public static float TREE_WIDTH = 100;
	public static float WOOD_HEIGHT = 100;
	int numberWood;

	public Tree(int numberWood) {
		this.numberWood = numberWood;
		setWidth(TREE_WIDTH);
		for (int i = 0; i < numberWood; i++) {
			Wood wood = new Wood(random.nextInt(3));
			System.out.println(wood.getKind());
			trees.add(wood);
			wood.setY(i * 100);
			addActor(wood);
		}
	}

	public boolean chopable = true;

	public void setChopable(boolean chopable) {
		this.chopable = chopable;
	}

	public Wood updateWhenChoped() {
		final Wood w = trees.get(0);
		if (chopable) {
			setChopable(false);
			System.out.println("y = " + getY());
			addAction(Actions.sequence(new Action() {

				@Override
				public boolean act(float delta) {
					trees.removeIndex(0);
					removeActor(w);
					return true;
				}
			}, Actions.moveTo(getX(), getY() - WOOD_HEIGHT, 0.1f), new Action() {

				@Override
				public boolean act(float delta) {
					Wood wood = new Wood(random.nextInt(3));
					trees.add(wood);
					setY(getY() + WOOD_HEIGHT);
					draw();
					setChopable(true);
					// setY(y + 100);
					return true;
				}
			}));
		}
		return w;
		// for (int i = 0; i < trees.size; i++) {
		// trees.set(i, trees.get(i + 1));
		// }
		// trees.removeIndex(trees.size - 1);
	}

	private void draw() {
		clear();
		for (int i = 0; i < trees.size; i++) {
			Wood wood = trees.get(i);
			System.out.println(wood.getKind());
			wood.setY(i * 100);
			addActor(wood);
		}
		System.out.println(trees.size + " trees size");
	}

}
